package com.urea.system.service.impl;

import java.util.List;
import java.util.Objects;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.urea.system.mapper.TerminalCommLogMapper;
import com.urea.system.domain.TerminalCommLog;
import com.urea.system.service.ITerminalCommLogService;

/**
 * 终端通信日志Service业务层处理
 *
 * @author liuzd
 * @date 2023-08-01
 */
@Service
public class TerminalCommLogServiceImpl extends ServiceImpl<TerminalCommLogMapper, TerminalCommLog> implements ITerminalCommLogService {
    @Autowired
    private TerminalCommLogMapper terminalCommLogMapper;

    /**
     * 查询终端通信日志
     *
     * @param id 终端通信日志主键
     * @return 终端通信日志
     */
    @Override
    public TerminalCommLog selectTerminalCommLogById(Long id) {
        return terminalCommLogMapper.selectTerminalCommLogById(id);
    }

    /**
     * 查询终端通信日志列表
     *
     * @param terminalCommLog 终端通信日志
     * @return 终端通信日志
     */
    @Override
    public List<TerminalCommLog> selectTerminalCommLogList(TerminalCommLog terminalCommLog) {
        return terminalCommLogMapper.selectTerminalCommLogList(terminalCommLog);
    }

    /**
     * 新增终端通信日志
     *
     * @param terminalCommLog 终端通信日志
     * @return 结果
     */
    @Override
    public int insertTerminalCommLog(TerminalCommLog terminalCommLog) {
        return terminalCommLogMapper.insertTerminalCommLog(terminalCommLog);
    }

    /**
     * 修改终端通信日志
     *
     * @param terminalCommLog 终端通信日志
     * @return 结果
     */
    @Override
    public int updateTerminalCommLog(TerminalCommLog terminalCommLog) {
        return terminalCommLogMapper.updateTerminalCommLog(terminalCommLog);
    }

    /**
     * 批量删除终端通信日志
     *
     * @param ids 需要删除的终端通信日志主键
     * @return 结果
     */
    @Override
    public int deleteTerminalCommLogByIds(Long[] ids) {
        return terminalCommLogMapper.deleteTerminalCommLogByIds(ids);
    }

    /**
     * 删除终端通信日志信息
     *
     * @param id 终端通信日志主键
     * @return 结果
     */
    @Override
    public int deleteTerminalCommLogById(Long id) {
        return terminalCommLogMapper.deleteTerminalCommLogById(id);
    }

    @Override
    public Boolean isRefueling(String terminalNo, Long timeout) {
        List<TerminalCommLog> terminalCommLogs = terminalCommLogMapper.refuelingCommLogs(terminalNo, timeout);
        if (CollUtil.isNotEmpty(terminalCommLogs)) {
            TerminalCommLog log = terminalCommLogs.get(0);
            List<TerminalCommLog> feeLogs = lambdaQuery().eq(TerminalCommLog::getTerminalNo, terminalNo).eq(TerminalCommLog::getCommType, "fee")
                    .eq(TerminalCommLog::getSerialNo,log.getSerialNo())
                    .orderByDesc(TerminalCommLog::getId).list();
            if (CollUtil.isNotEmpty(feeLogs)){
                return false;
            }
        }
        return CollUtil.isNotEmpty(terminalCommLogs);
    }
}
